package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.ParseException;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class FindWay extends Activity {
	ArrayList<Float> r = new ArrayList<Float>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		r = getLocation();
		float latitude = r.get(0);
		float longitude = r.get(1);
		// setContentView(R.layout.activity_find_way);
		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse("http://maps.google.com/maps?"
						+ "saddr=latitude,longitude" + "&daddr=" + latitude
						+ "," + longitude));
		intent.setClassName("com.google.android.apps.maps",
				"com.google.android.maps.MapsActivity");

		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_find_way, menu);
		return true;
	}

	private ArrayList<Float> getLocation() {
		int id_Location = 0;
		try {
			id_Location = ListObject.obj.getInt("id_Location");

			JSONObject json_data;
			ArrayList<Float> r = new ArrayList<Float>();
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("id_Location", Integer
					.toString(id_Location)));
			String result = null;

			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/LocationController/getLocation", postParameters);
			JSONArray jArray = new JSONArray(result);
			for (int i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				// Log.d("A", json_data.getString("name"));
				r.add(Float.parseFloat(json_data.getString("latitude")));
				r.add(Float.parseFloat(json_data.getString("longitude")));
			}
			// return r;
		} catch (JSONException e1) {
			// Toast.makeText(((ContextWrapper) activity).getBaseContext(),
			// e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (ParseException e1) {
			// Toast.makeText(((ContextWrapper) activity).getBaseContext(),
			// e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return r;

	}

}
